package com.ruoyi.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Base64;
import java.util.Base64.Decoder;

/**
 * 用于转换 base64图片
 *
 * @Author qiqi
 * @Date 2022/6/8 17:41
 * @Version 1.0
 */
public class BASE64DecodedMultipartFileUtil implements MultipartFile {
	private Logger log = LoggerFactory.getLogger(getClass());
    private final byte[] imgContent;
    private final String header;

    public BASE64DecodedMultipartFileUtil(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }

    /**
     * 获取名字
     * <p>Title: getName</p>   
     * <p>Description: </p>   
     * @return   
     * @see MultipartFile#getName()
     */
    @Override
    public String getName() {
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    /**
     * 获取原始名字
     * <p>Title: getOriginalFilename</p>   
     * <p>Description: </p>   
     * @return   
     * @see MultipartFile#getOriginalFilename()
     */
    @Override
    public String getOriginalFilename() {
        return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
    }

    /**
     * 类型
     * <p>Title: getContentType</p>   
     * <p>Description: </p>   
     * @return   
     * @see MultipartFile#getContentType()
     */
    @Override
    public String getContentType() {
        return header.split(":")[1];
    }

    /**
     * 判断是否为空
     * <p>Title: isEmpty</p>   
     * <p>Description: </p>   
     * @return   
     * @see MultipartFile#isEmpty()
     */
    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    /**
     * 大小
     * <p>Title: getSize</p>   
     * <p>Description: </p>   
     * @return   
     * @see MultipartFile#getSize()
     */
    @Override
    public long getSize() {
        return imgContent.length;
    }

    /**
     * 获取bytes
     * <p>Title: getBytes</p>   
     * <p>Description: </p>   
     * @return
     * @throws IOException   
     * @see MultipartFile#getBytes()
     */
    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }

    /**
     * 获取输入
     * <p>Title: getInputStream</p>   
     * <p>Description: </p>   
     * @return
     * @throws IOException   
     * @see MultipartFile#getInputStream()
     */
    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }

    /**
     * 转化
     * <p>Title: transferTo</p>   
     * <p>Description: </p>   
     * @param dest
     * @throws IOException
     * @throws IllegalStateException   
     * @see MultipartFile#transferTo(File)
     */
	@Override
	public void transferTo(File dest) throws IOException, IllegalStateException {
		OutputStream stream = null;
		try {
			stream = new FileOutputStream(dest);
			stream.write(imgContent);
		} catch (Exception e) {
			log.info("{}", ExceptionUtil.getExceptionMessage(e));
		} finally {
			try {
				if (stream != null) {
					stream.close();
				}
			} catch (IOException e) {
				log.info("{}", ExceptionUtil.getExceptionMessage(e));
			}
		}
	}

	/**
	 * 文件
	 * <p>Title: base64ToMultipart</p>  
	 * <p>Description: </p>  
	 * @param base64
	 * @return
	 */
    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");
            Decoder decoder = Base64.getDecoder();
            byte[] b = decoder.decode(baseStrs[1]);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new BASE64DecodedMultipartFileUtil(b, baseStrs[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
